package cn.edu.cdu.health.controller;

import cn.edu.cdu.health.entity.Diagnosis;
import cn.edu.cdu.health.entity.PageDto;
import cn.edu.cdu.health.entity.PageResult;
import cn.edu.cdu.health.common.R;
import cn.edu.cdu.health.service.DiagnosisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/diagnoses")
public class DiagnosisController {

    @Autowired
    private DiagnosisService diagnosisService;

    // 新增诊断记录
    @PostMapping
    public R<String> addDiagnosis(@RequestBody Diagnosis diagnosis) {
        boolean saved = diagnosisService.save(diagnosis);
        return saved ? R.success("新增成功") : R.error("新增失败");
    }

    // 分页查询诊断记录
    @GetMapping
    public R<PageResult<Diagnosis>> getDiagnosisPage(PageDto pageDto) {
        PageResult<Diagnosis> result = diagnosisService.getDiagnosisPage(pageDto);
        return R.success(result);
    }

    // 更新诊断记录
    @PutMapping("/{id}")
    public R<String> updateDiagnosis(@PathVariable Long id, @RequestBody Diagnosis diagnosis) {
        diagnosis.setId(id);
        boolean updated = diagnosisService.updateById(diagnosis);
        return updated ? R.success("更新成功") : R.error("更新失败");
    }
}